/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.SubCauseDao;
import com.rbh.risk.entity.MainCause;
import com.rbh.risk.entity.SubCause;
import com.rbh.risk.support.SessionFactorySupport;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("subCauseDao")
public class SubCauseDaoImpl extends SessionFactorySupport implements SubCauseDao {

    @Override
    public void insert(SubCause subCause) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().save(subCause);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(SubCause subCause) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().update(subCause);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(SubCause subCause) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(subCause);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public SubCause getById(Integer subCauseId) throws DataAccessException {
        SubCause result = null;
        try {
            result = (SubCause) super.getSessionFactory().getCurrentSession().get(SubCause.class, subCauseId.shortValue());
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<SubCause> getByMainCause(MainCause mainCause) throws DataAccessException {
        List<SubCause> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(SubCause.class);
            criteria.add(Restrictions.disjunction()
                    .add(Restrictions.eq("mainCause", mainCause))
                    .add(Restrictions.isNull("mainCause")));
            results = criteria.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<SubCause> getAll() throws DataAccessException {
        List<SubCause> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + SubCause.class.getName());
            results = query.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }
}
